(function () {
    'use strict';

    angular
        .module('collect.plans.controller', [])
        .controller('CollectPlansController', CollectPlansController);

    CollectPlansController.$inject = ['$scope','$yikeUtils','$state','$ionicHistory','$ionicTabsDelegate','$timeout','localStorageService','$ionicLoading','$ionicScrollDelegate','$ionicPopup','$ionicModal','$rootScope'];
    /* @ngInject */
    function CollectPlansController($scope,$yikeUtils,$state,$ionicHistory,$ionicTabsDelegate,$timeout,localStorageService,$ionicLoading,$ionicScrollDelegate,$ionicPopup,$ionicModal,$rootScope){
        $scope.user=localStorageService.get('user');
        //console.log($scope.user);
        init();
        $scope.openActive = openActive;
        $scope.openPlansDetail = openPlansDetail;
        $scope.addCollect = addCollect;
        $scope.deleteCollect = deleteCollect;
        $scope.backpage = backpage;
        $scope.selectPlans = selectPlans;
        /*存储需要添加的计划*/
        $scope.collectArr= [];
        /*需要删除的计划*/
        $scope.delArr = [];




        $scope.addDate = addDate;
        function  addDate(data){
            //console.log(data);
            $scope.lsitRow = data.row;
            //console.log($scope.lsitRow);
        }
        /*初始化项目*/
        function init(){
            pkScheme();
            myCollect();
            /*初始化计划的状态*///这里不这样做，用获取的对象存储对应的状态
            $scope.plans = {
                activeState:false
            };
            /*$ionicModal.fromTemplateUrl('plans-details.html', {
             scope:$scope,
             animation:"slide-in-up"
             }).then(function(modal){
             $scope.detailsPlans = modal;
             });
             $scope.openDetailsPlans = function(){
             $scope.detailsPlans.show();
             };
             $scope.closeDetailsPlans = function(){
             $scope.detailsPlans.hide();
             }*/
        }
        /*会进入全部计划还是我的计划*/
        $scope.selectState = true;
        function selectPlans(){
            $scope.selectState = !$scope.selectState;
        }
        //选择是我的计划还是所有计划
        $scope.choice = choice;
        $scope.choiceStatus = 1;
        function choice(state){
            $scope.choiceStatus = state;
        }


        /*选中或者取消的状态*/
        function openActive(item,addOrDel){
            //console.log(item,addOrDel);
            item.state = !item.state;

            $scope.states = item.id;
            //console.log($scope.states);

            //$scope.playsState = !item.playsState;
            //
            //console.log(item.playsState);

            //$scope.childData = item.row;

            //console.log($scope.childData);
            if(addOrDel == "add"){
                if(item.state){
                    if($scope.collect.length != 0){
                        for(var x=0,l=$scope.collect.length;x<l;x++){
                            if($scope.collect[x].plan_id == item.id){
                                item.state = false;
                                /*$yikeUtils.toast("此玩法已收藏，请勿重复收藏");*/
                                //console.log("这里显示对应的标签提示");
                                return;
                            }
                        }
                    }
                    $scope.collectArr.push(item.id);
                }else{
                    for(var i=0,l=$scope.collectArr.length;i<l;i++){
                        if($scope.collectArr[i] == item.id){
                            $scope.collectArr.splice(i,1);
                        }
                    }
                }
            }
            if(addOrDel == "del"){
                openPlansDetail(item.plan_id);
                if(item.state){
                    $scope.delArr.push(item.id);
                }else{
                    for(var i=0,l=$scope.delArr.length;i<l;i++){
                        if($scope.delArr[i] == item.id){
                            $scope.delArr.splice(i,1);
                        }
                    }
                }
            }
        }
        function backpage(){
            if($rootScope.buytype == 1){
                $state.go('tab.home');
            }else{
                $state.go('tab.account');
            }
        }
        /*长按触发的时间*/
        function openPlansDetail(play_id){
            $ionicLoading.show({
                template: '<ion-spinner icon="bubbles"></ion-spinner>'
            });
            yikeTaishan.detailsList($scope.user.id,play_id)
                .then(function(data){
                    $ionicLoading.hide();
                    $scope.detailsMessage = data.result.collection;

                    /*$scope.openDetailsPlans();*/
                    $scope.$digest();
                });
        }
        /*获取全部系列*/
        $scope.itemsList=[];
        function pkScheme() {
            yikeTaishan.pkScheme($scope.user.token)
                .then(function (data) {
                    //console.log(data);
                    if(data.status == 1){
                        for(var key in data.result.plan){
                            data.result.plan[key].state = false;
                            data.result.plan[key].playsState = false;
                            data.result.plan[key].collected = false;
                            data.result.plan[key].id_img = data.result.plan[key].id-1;
                            $scope.itemsList.push(data.result.plan[key].row);
                        }
                        $scope.allplans = data.result.plan;
                        //console.log($scope.allplans);
                    }
                    $scope.$digest();
                    $ionicLoading.hide();
                })
        }
        //添加收藏方案
        function addCollect(id) {
            yikeTaishan.expire($scope.user.id,$scope.user.token)
                .then(function (data) {

                    //console.log(data);

                    if(data.status == 1){
                        //if($scope.collectArr.length == 0){
                        //    $yikeUtils.toast("请选择要收藏的系列");
                        //    return;
                        //}
                        //yikeTaishan.collect('add','2',$scope.collectArr.join(","),$scope.user.id)
                        yikeTaishan.collect('add','2',id,$scope.user.id)
                            .then(function (data) {
                                //console.log(data);
                                $ionicLoading.hide();
                                if(data.status == 1){
                                    $yikeUtils.toast(data.result.result);
                                    $scope.isShow=false;
                                    $scope.$digest();
                                }else{
                                    $yikeUtils.toast(data.result.result);
                                    $scope.$digest();
                                }
                                $scope.$digest();
                                //$scope.collectArr.length = 0;
                                myCollect(data.result.result);
                                pkScheme();
                            })
                    } else{
                        clearInterval(pksh);
                        var alertPopup = $ionicPopup.alert({
                            title: '提示',
                            template: data.result.result,
                            buttons:[{
                                text:'确定',
                                type: 'button-positive'
                            }]
                        });
                        alertPopup.then(function() {
                            $ionicLoading.hide();
                            localStorageService.remove('user');
                            $state.go('login')
                        });
                    }
                });
            $scope.$$phase || $scope.$digest();
        }
        //我的收藏
        function myCollect(message) {
            $ionicLoading.show({
                template: '<ion-spinner icon="bubbles"></ion-spinner>'
            });
            yikeTaishan.expire($scope.user.id,$scope.user.token)
                .then(function (data) {
                    if(data.status == 1){
                        yikeTaishan.myCollect('my_collection','pk10',$scope.user.id)
                            .then(function (data) {
                                $ionicLoading.hide();
                                if(data.status == 1){
                                    if(data.result.collection instanceof Object){
                                        for(var i=0,l=data.result.collection.length;i<l;i++){
                                            data.result.collection[i].state = false;
                                            data.result.collection[i].id_img = data.result.collection[i].plan_id-1;
                                        }
                                    }
                                    $scope.collect=data.result.collection;

                                    //console.log($scope.collect);

                                    for(var a in $scope.allplans){
                                        for(var c=0;c<$scope.collect.length;c++){
                                            if($scope.collect[c].play_id == $scope.allplans[a].id){
                                                $scope.allplans[a].collected = true;
                                            }
                                        }
                                    }
                                    /*当为了收藏和删除收藏显示的信息*/
                                    if(message){
                                        for(var m=0,l1=$scope.allplans.length;m<l1;m++){
                                            $scope.allplans[m].state = false;
                                        }
                                        $yikeUtils.toast(message);
                                    }
                                    $scope.$digest();
                                }else{
                                    $scope.collect = [];
                                    $yikeUtils.toast(data.result.collection);
                                }
                                $scope.$digest();
                            })
                    }else{
                        var alertPopup = $ionicPopup.alert({
                            title: '提示',
                            template: data.result.result,
                            buttons:[{
                                text:'确定',
                                type: 'button-positive'
                            }]
                        });
                        alertPopup.then(function() {
                            $ionicLoading.hide();
                            localStorageService.remove('user');
                            $state.go('login')
                        });
                    }
                })
        }
        //删除收藏
        function deleteCollect(id) {
            yikeTaishan.expire($scope.user.id,$scope.user.token)
                .then(function (data) {
                    if(data.status == 1){
                        //yikeTaishan.deleteCollect('delete','pc_pk10',$scope.delArr.join(","))
                        yikeTaishan.deleteCollect('delete','pc_pk10',id)
                            .then(function (data) {
                                //console.log(data);
                                $ionicLoading.hide();
                                $scope.$digest();
                                //$scope.delArr.length = 0;
                                myCollect(data.result.result);
                                pkScheme();

                            })
                    }else{
                        var alertPopup = $ionicPopup.alert({
                            title: '提示',
                            template: data.result.result,
                            buttons:[{
                                text:'确定',
                                type: 'button-positive'
                            }]
                        });
                        alertPopup.then(function() {
                            $ionicLoading.hide();
                            localStorageService.remove('user');
                            $state.go('login')
                        });
                    }
                })
        }


        //假数据
        $scope.share = function() {
            alert(12312);
        };



    }
})();
